--- /dev/null
+<?
+# Blocks and bans object
+
+#TODO: This could be used everywhere, but it isn't.
+
+# All the functions in this class assume the object is either explicitly
+# loaded or filled. It is not load-on-demand.
+
+# To use delete(), you only need to fill $mAddress
+
+class Block
+{
+ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp;
+
+ function Block( $address = "", $user = "", $by = 0, $reason = "", $timestamp = "" )
+ {
+ $this->mAddress = $address;
+ $this->mUser = $user;
+ $this->mBy = $by;
+ $this->mReason = $reason;
+ $this->mTimestamp = $timestamp;
+ }
+
+ /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
+ {
+ $ban = new Block();
+ $ban->load( $address, $user, $killExpired );
+ return $ban;
+ }
+
+ function clear()
+ {
+ $mAddress = $mReason = $mTimestamp = "";
+ $mUser = $mBy = 0;
+ }
+
+ # Get a ban from the DB, with either the given address or the given username
+ function load( $address, $user = 0, $killExpired = true )
+ {
+ $fname = "Block::load";
+ $ret = false;
+ $killed = false;
+
+ if ( 0 == $user ) {
+ $sql = "SELECT * FROM ipblocks WHERE ipb_address='$address'";
+ } else {
+ $sql = "SELECT * FROM ipblocks WHERE (ipb_address='$address' OR ipb_user={$user})";
+ }
+
+
+ $res = wfQuery( $sql, $fname );
+ if ( 0 == wfNumRows( $res ) ) {
+ # User is not blocked
+ $this->clear();
+ } else {
+ # Get first block
+ $row = wfFetchObject( $res );
+ $this->initFromRow( $row );
+
+ if ( $killExpired ) {
+
+ # If requested, delete expired rows
+ do {
+ $killed = $this->deleteIfExpired();
+ $row = wfFetchObject( $res );
+ } while ( $killed && $row );
+
+ # If there were any left after the killing finished, return true
+ if ( $row == false ) {
+ $ret = false;
+ $this->clear();
+ } else {
+ $ret = true;
+ }
+ } else {
+ $ret = true;
+ }
+ }
+ wfFreeResult( $res );
+ return $ret;
+ }
+
+ function initFromRow( $row )
+ {
+ $this->mAddress = $row->ipb_address;
+ $this->mReason = $row->ipb_reason;
+ $this->mTimestamp = $row->ipb_timestamp;
+ $this->mUser = $row->ipb_user;
+ $this->mBy = $row->ipb_by;
+ }
+
+ # Callback with a Block object for every block
+ /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
+ {
+ $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp";
+ $res = wfQuery( $sql, "Block::enumBans" );
+ $block = new Block();
+
+ while ( $row = wfFetchObject( $res ) ) {
+ $block->initFromRow( $row );
+ if ( $killExpired ) {
+ if ( !$block->deleteIfExpired() ) {
+ $callback( $block, $tag );
+ }
+ } else {
+ $callback( $block, $tag );
+ }
+ }
+ wfFreeResult( $res );
+ }
+
+ function delete()
+ {
+ wfQuery( "DELETE FROM ipblocks WHERE ipb_address='{$this->mAddress}'",
+ "Block::delete" );
+ }
+
+ function insert()
+ {
+ $sql = "INSERT INTO ipblocks (ipb_address, ipb_user, ipb_by, " .
+ "ipb_reason, ipb_timestamp ) VALUES ('{$this->mAddress}', {$this->mUser}, " .
+ "{$this->mBy}, '" . wfStrencode( $this->mReason ) . "','{$this->mTimestamp}')";
+ wfQuery( $sql, "Block::insert" );
+ }
+
+ function deleteIfExpired()
+ {
+ if ( $this->isExpired() ) {
+ $this->delete();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function isExpired()
+ {
+ global $wgIPBlockExpiration, $wgUserBlockExpiration;
+
+ $period = $this->mUser ? $wgUserBlockExpiration : $wgIPBlockExpiration;
+
+ # Period==0 means no expiry
+ if ( !$period ) {
+ return false;
+ }
+ $expiry = wfTimestamp2Unix( $this->mTimestamp ) + $period;
+ $now = wfTimestamp2Unix( wfTimestampNow() );
+ if ( $now > $expiry ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function isValid()
+ {
+ return $this->mAddress != "";
+ }
+
+
+ function updateTimestamp()
+ {
+ $sql = "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . "' WHERE ipb_address='{$this->mAddress}'";
+ wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() .
+ "' WHERE ipb_address='{$this->mAddress}'", "Block::updateTimestamp" );
+ }
+}
+?>
# Miscellaneous configuration settings
#
-$wgReadOnlyFile = "{$wgUploadDirectory}/lock_yBgMBwiR";
-$wgDebugLogFile = "{$wgUploadDirectory}/log_dlJbnMZb";
-$wgDebugComments = false;
-$wgReadOnly = false;
-$wgSqlLogFile = "{$wgUploadDirectory}/sqllog_mFhyRe6";
-$wgLogQueries = false;
+$wgReadOnlyFile = "{$wgUploadDirectory}/lock_yBgMBwiR";
+$wgDebugLogFile = "{$wgUploadDirectory}/log_dlJbnMZb";
+$wgDebugComments = false;
+$wgReadOnly = false;
+$wgSqlLogFile = "{$wgUploadDirectory}/sqllog_mFhyRe6";
+$wgLogQueries = false;
$wgUseBetterLinksUpdate = true;
-
+$wgSysopUserBans = true; # Allow sysops to ban logged-in users
+$wgIPBlockExpiration = 86400; # IP blocks expire after this many seconds, 0=infinite
+$wgUserBlockExpiration = 0; # As above, but for logged-in users
# The following three config variables are used to define
# the rights of users in your system.
include_once( "$IP/Article.php" );
include_once( "$IP/MagicWord.php" );
include_once( "$IP/MemCachedClient.inc.php" );
+include_once( "$IP/Block.php" );
global $wgUser, $wgLang, $wgOut, $wgTitle;
global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
function doSubmit()
{
global $wgOut, $wgUser, $wgLang;
- global $ip, $wpBlockAddress, $wpBlockReason;
+ global $ip, $wpBlockAddress, $wpBlockReason, $wgSysopUserBlocks;
$fname = "IPBlockForm::doSubmit";
$userId = 0;
if ( ! preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/",
$wpBlockAddress ) )
{
- $userId = User::idFromName( $wpBlockAddress );
- if ( $userId == 0 ) {
+ if ( $wgSysopUserBlocks ) {
+ $userId = User::idFromName( $wpBlockAddress );
+ if ( $userId == 0 ) {
+ $this->showForm( wfMsg( "badipaddress" ) );
+ return;
+ }
+ } else {
$this->showForm( wfMsg( "badipaddress" ) );
return;
- }
+ }
}
if ( "" == $wpBlockReason ) {
$this->showForm( wfMsg( "noblockreason" ) );
function showList( $msg )
{
- global $wgOut, $wgUser, $wgLang;
- global $ip;
-
+ global $wgOut;
+
$wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
if ( "" != $msg ) {
$wgOut->setSubtitle( $msg );
}
- $sql = "SELECT ipb_timestamp,ipb_address,ipb_user,ipb_by,ipb_reason " .
- "FROM ipblocks ORDER BY ipb_timestamp";
- $res = wfQuery( $sql, "IPUnblockForm::showList" );
-
$wgOut->addHTML( "<ul>" );
- $sk = $wgUser->getSkin();
- while ( $row = wfFetchObject( $res ) ) {
- $addr = $row->ipb_address;
- $name = User::whoIs( $row->ipb_by );
- $ulink = $sk->makeKnownLink( $wgLang->getNsText( Namespace::getUser() ). ":{$name}", $name );
- $d = $wgLang->timeanddate( $row->ipb_timestamp, true );
-
- $line = str_replace( "$1", $d, wfMsg( "blocklistline" ) );
- $line = str_replace( "$2", $ulink, $line );
- $line = str_replace( "$3", $row->ipb_address, $line );
-
- $wgOut->addHTML( "<li>{$line}" );
- $clink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
- "Contributions" ), "target={$addr}" ) . "\">" .
- wfMsg( "contribslink" ) . "</a>";
- $wgOut->addHTML( " ({$clink})" );
-
- if ( $wgUser->isSysop() ) {
- $ublink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
- "Ipblocklist" ), "action=unblock&ip={$addr}" ) . "\">" .
- wfMsg( "unblocklink" ) . "</a>";
- $wgOut->addHTML( " ({$ublink})" );
- }
- if ( "" != $row->ipb_reason ) {
- $wgOut->addHTML( " <em>(" . wfEscapeHTML( $row->ipb_reason ) .
- ")</em>" );
- }
- $wgOut->addHTML( "</li>\n" );
- }
- wfFreeResult( $res );
+ Block::enumBlocks( "wfAddRow", 0 );
$wgOut->addHTML( "</ul>\n" );
}
}
+# Callback function
+function wfAddRow( $block, $tag ) {
+ global $wgOut, $wgUser, $wgLang, $ip;
+
+ $sk = $wgUser->getSkin();
+ $addr = $block->mAddress;
+ $name = User::whoIs( $block->mBy );
+ $ulink = $sk->makeKnownLink( $wgLang->getNsText( Namespace::getUser() ). ":{$name}", $name );
+ $d = $wgLang->timeanddate( $block->mTimestamp, true );
+
+ $line = str_replace( "$1", $d, wfMsg( "blocklistline" ) );
+ $line = str_replace( "$2", $ulink, $line );
+ $line = str_replace( "$3", $block->mAddress, $line );
+
+ $wgOut->addHTML( "<li>{$line}" );
+ $clink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
+ "Contributions" ), "target={$addr}" ) . "\">" .
+ wfMsg( "contribslink" ) . "</a>";
+ $wgOut->addHTML( " ({$clink})" );
+
+ if ( $wgUser->isSysop() ) {
+ $ublink = "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
+ "Ipblocklist" ), "action=unblock&ip={$addr}" ) . "\">" .
+ wfMsg( "unblocklink" ) . "</a>";
+ $wgOut->addHTML( " ({$ublink})" );
+ }
+ if ( "" != $block->mReason ) {
+ $wgOut->addHTML( " <em>(" . wfEscapeHTML( $block->mReason ) .
+ ")</em>" );
+ }
+ $wgOut->addHTML( "</li>\n" );
+}
+
+
?>
{
if ( -1 != $this->mBlockedby ) { return; }
- $remaddr = getenv( "REMOTE_ADDR" );
- if ( 0 == $this->mId ) {
- $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
- "ipb_address='$remaddr'";
- } else {
- $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
- "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
- }
- $res = wfQuery( $sql, "User::getBlockedStatus" );
- if ( 0 == wfNumRows( $res ) ) {
+ $ban = new Ban();
+ if ( $ban->load( getenv( "REMOTE_ADDR" ), $this->mId ) ) {
$this->mBlockedby = 0;
return;
}
- $s = wfFetchObject( $res );
- $this->mBlockedby = $s->ipb_by;
- $this->mBlockreason = $s->ipb_reason;
+
+ $this->mBlockedby = $ban->by;
+ $this->mBlockreason = $ban->reason;
}
function isBlocked()
"user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
"user_email= '" . wfStrencode( $this->mEmail ) . "', " .
"user_options= '" . $this->encodeOptions() . "', " .
- "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
-.
+ "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
"user_touched= '" . wfStrencode( $this->mTouched ) .
"' WHERE user_id={$this->mId}";
wfQuery( $sql, "User::saveSettings" );
# that they successfully log on from.
$fname = "User::spreadBlock";
- if ( $this->mId == 0 || !$this->isBlocked()) {
+ wfDebug( "User:spreadBlock()\n" );
+ if ( $this->mId == 0 ) {
return;
}
- $sql = "SELECT * FROM ipblocks WHERE ipb_user={$this->mId}";
- $res = wfQuery( $sql, $fname );
- if ( wfNumRows( $res ) == 0 ) {
+ $userblock = Block::newFromDB( "", $this->mId );
+ if ( !$userblock->isValid() ) {
return;
}
# Check if this IP address is already blocked
$addr = getenv( "REMOTE_ADDR" );
- $sql = "SELECT * FROM ipblocks WHERE ipb_address='{$addr}'";
- $res2 = wfQuery( $sql, $fname );
- if ( wfNumRows( $res2 ) != 0 ) {
+ $ipblock = Block::newFromDB( $addr );
+ if ( $ipblock->isValid() ) {
+ # Just update the timestamp
+ $ipblock->updateTimestamp();
return;
}
- $row = wfFetchObject( $res );
- $reason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
- $reason = str_replace( "$2", $row->ipb_reason, $reason );
-
- $addr = getenv( "REMOTE_ADDR" );
- $sql = "INSERT INTO ipblocks(ipb_address, ipb_user, ipb_by, " .
- "ipb_reason, ipb_timestamp) VALUES ('{$addr}', 0, {$row->ipb_by}," .
- "'{$reason}', '" . wfTimestampNow() . "')";
- wfQuery( $sql, $fname );
-
- wfFreeResult( $res );
- wfFreeResult( $res2 );
+ # Make a new ban object with the desired properties
+ wfDebug( "Autoblocking {$this->mUserName}@{$addr}\n" );
+ $ipblock->mAddress = $addr;
+ $ipblock->mUser = 0;
+ $ipblock->mBy = $userblock->mBy;
+ $ipblock->mReason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
+ $ipblock->mReason = str_replace( "$2", $userblock->mReason, $ipblock->mReason );
+ $ipblock->mTimestamp = wfTimestampNow();
+
+ # Insert it
+ $ipblock->insert();
+
}
}